package com.vip.base.controller;


import com.vip.base.service.ISysRoleService;
import com.vip.common.annotation.LogCollection;
import com.vip.common.constant.LogConsts;
import com.vip.common.constant.UrlConsts;
import com.vip.common.response.ErrorCode;
import com.vip.common.response.RestResponse;
import com.vip.common.response.RestResponses;
import com.vip.database.dto.role.AddRoleDTO;
import com.vip.database.dto.role.GetRoleDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 *  角色表前端控制器
 *
 * @author LEON
 * @since 2020-03-28
 */
@RestController
@RequestMapping(UrlConsts.API + "/role")
public class SysRoleController {
    @Autowired
    private ISysRoleService roleService;

    /**
     * 添加新角色
     *
     * @return
     */
    @PreAuthorize("hasAuthority('perm:add')")
    @LogCollection(object = LogConsts.OBJ_ROLE)
    @PostMapping
    public RestResponse<String> addNewRole(@RequestBody @Valid AddRoleDTO dto, BindingResult bindingResult){
        if (bindingResult.hasErrors()){
            return RestResponses.newFailResponse(ErrorCode.INVALID_PARAMETER, bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return RestResponses.newResponseFromResult(roleService.addNewRole(dto));
    }

    /**
     * 分页获取角色列表数据
     *
     * @param pageNo 当前页
     * @param pageSize 当前页大小
     * @return 当前页列表数据
     */
    @GetMapping(value = "/list")
    public RestResponse<List<GetRoleDTO>> getRoleList(@RequestParam Integer pageNo, @RequestParam Integer pageSize){
        return RestResponses.newResponseFromResult(roleService.getRoleList(pageNo,pageSize));
    }

    /**
     * 获取所有角色
     *
     * @return
     */
    @GetMapping(value = "/all")
    public RestResponse<List<GetRoleDTO>> getAllRoles(){
        return RestResponses.newResponseFromResult(roleService.getAllRoles());
    }

    /**
     * 获取角色详情
     *
     * @param roleId 角色ID
     * @return
     */
    @GetMapping(value = "/{roleId}")
    public RestResponse<GetRoleDTO> getRoleDetails(@PathVariable String roleId){
        return RestResponses.newResponseFromResult(roleService.getRoleDetailsByRoleId(roleId));
    }

    /**
     * 更新角色信息
     *
     * @param roleDto
     * @return
     */
    @LogCollection(object = LogConsts.OBJ_ROLE)
    @PutMapping
    public RestResponse<String> updateRole(@RequestBody @Valid AddRoleDTO roleDto, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return RestResponses.newFailResponse(ErrorCode.INVALID_PARAMETER, bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        return RestResponses.newResponseFromResult(roleService.updateRole(roleDto));
    }

    /**
     * 角色授权：为角色添加/删除权限
     *
     * @param roleId
     * @param permission
     * @param type add、delete
     * @return
     */
    @LogCollection(object = LogConsts.OBJ_ROLE)
    @PutMapping(value = "/accredit/{roleId}/{type}")
    public RestResponse<String> allotPermission(@PathVariable("roleId") String roleId, @PathVariable("type") String type, @RequestParam String permission) {
        return RestResponses.newResponseFromResult(roleService.allotPermission(roleId, type, permission));
    }
}

